package com.syq.dao.impl;

import com.syq.bean.Menu;
import com.syq.dao.MenuDao;
import com.syq.util.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends DBUtils implements MenuDao {
    @Override
    public List<Menu> getMenuList() {
        List<Menu> menuList = new ArrayList<>();
        try {
            String sql = "select * from menu";
            ResultSet resultSet = query(sql, null);
            while (resultSet.next()) {
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));

                menuList.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return menuList;
    }

    @Override
    public List<Menu> getMenuList(int pageIndex, int pageSize) {
        List<Menu> menuList = new ArrayList<>();
        try {
            String sql = "select * from menu limit ?,?";
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            resultSet = query(sql,params);
            while (resultSet.next()) {
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setState(resultSet.getInt("state"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));

                menuList.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return menuList;
    }

    @Override
    public int total() {
        int toatal = 0;
        try {
            String  sql = "select count(1) from menu  ";
            resultSet = query(sql,null);
            while (resultSet.next()) {
                toatal = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return toatal;
    }

    @Override
    public Menu getMenuById(int menuId) {
        Menu menu = new Menu();
        try {
            String sql = "select * from menu where menuid=?";
            List params = new ArrayList();
            params.add(menuId);
            resultSet = query(sql,params);
            while (resultSet.next()) {
                menu.setMenuId(menuId);
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menu.setUrl(resultSet.getString("url"));
                menu.setState(resultSet.getInt("state"));
                menu.setDesc(resultSet.getString("desc"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return menu;
    }

    @Override
    public int updateMenu(int menuId) {
        return 0;
    }

//    @Override
//    public Menu getUpMenu(int menuId) {
//        Menu menu = new Menu();
//        try {
//            String sql = "select  from menu where menuid=?";
//            List params = new ArrayList();
//            params.add(menuId);
//            resultSet = query(sql,params);
//            while (resultSet.next()) {
//                menu.setMenuId(menuId);
//                menu.setMenuName(resultSet.getString("menuname"));
//                menu.setUpmenuId(resultSet.getInt("upmenuid"));
//                menu.setUrl(resultSet.getString("url"));
//                menu.setState(resultSet.getInt("state"));
//                menu.setDesc(resultSet.getString("desc"));
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            closeAll();
//        }
//        return menu;
//    }
}
